package org.jeecg.modules.demo.kcmx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.kcmx.entity.JxcKcmx;
import org.jeecg.modules.demo.kcmx.service.IJxcKcmxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

 /**
 * @Description: 库存明细
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Api(tags="库存明细")
@RestController
@RequestMapping("/kcmx/jxcKcmx")
@Slf4j
public class JxcKcmxController extends JeecgController<JxcKcmx, IJxcKcmxService> {
	@Autowired
	private IJxcKcmxService jxcKcmxService;

	/**
	 * 分页列表查询
	 *
	 * @param jxcKcmx
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "库存明细-分页列表查询")
	@ApiOperation(value="库存明细-分页列表查询", notes="库存明细-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JxcKcmx>> queryPageList(JxcKcmx jxcKcmx,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JxcKcmx> queryWrapper = QueryGenerator.initQueryWrapper(jxcKcmx, req.getParameterMap());
		Page<JxcKcmx> page = new Page<JxcKcmx>(pageNo, pageSize);
		IPage<JxcKcmx> pageList = jxcKcmxService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	//获取编号
	@GetMapping(value = "/getbh")
	public  Result<List<String>> getList(String companyId){
		return Result.OK(jxcKcmxService.getbh(companyId));
	}
	 //获取结算方式
	 @GetMapping(value = "/getJS")
	 public  Result<List<String>> getJSList(String companyId){
		 return Result.OK(jxcKcmxService.getJSList(companyId));
	 }
	 //添加结算方式
	 @GetMapping(value = "/setjsfs/{jsfs1}")
	 public Result<String> setjsfs(@PathVariable String jsfs1, String companyId){

		 Date date = new Date();
		 SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		 String id = "ZC"+df2.format(date);
		 jxcKcmxService.setjsfs(jsfs1,id, companyId);
		 return Result.OK("添加成功！");
	 }
	 //获取交易方
	 @GetMapping(value = "/getcompany")
	 public  Result<List<String>> getcompanyList(String companyId){
		 return Result.OK(jxcKcmxService.getcompanyList(companyId));
	 }
	 //添加交易方
	 @GetMapping(value = "/setcompany/{company1}")
	 public Result<String> setcompany(@PathVariable String company1, String companyId){
		 Date date = new Date();
		 SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		 String id = "ZC"+df2.format(date);
		 jxcKcmxService.setcompany(company1,id, companyId);
		 return Result.OK("添加成功！");
	 }
	 //获取应付总额
	 @GetMapping(value = "/getyfsum")
	 public  Result<String> getyfsum(String companyId){
		 List<String> amounts = jxcKcmxService.getyfkList(companyId);
		 // 使用Stream API计算总金额
		 double total = amounts.stream()
				 .mapToDouble(Double::parseDouble) // 将每个字符串转换为Double
				 .sum(); // 计算所有Double值的总和

		 System.out.println("总金额为: " + total);
		 return Result.OK(String.valueOf(total));
	 }
	 //获取应收总额
	 @GetMapping(value = "/getyssum")
	 public  Result<String> getyssum(String companyId){
		 List<String> amounts = jxcKcmxService.getyskList(companyId);
		 // 使用Stream API计算总金额
		 double total = amounts.stream()
				 .mapToDouble(Double::parseDouble) // 将每个字符串转换为Double
				 .sum(); // 计算所有Double值的总和

		 System.out.println("总金额为: " + total);
		 return Result.OK(String.valueOf(total));
	 }
	 //获取库存明细总额
	 @GetMapping(value = "/getkcmxsum")
	 public  Result<String> getkcmxsum(String companyId){
		 Double totalall = 0.0;
		 QueryWrapper<JxcKcmx> queryWrapper = new QueryWrapper();
		 queryWrapper.eq("company_id", companyId);
		 List<JxcKcmx> jxcKcmx = jxcKcmxService.list(queryWrapper);
		 //统计库存的总额=库存*单价
		 for (int i = 0; i < jxcKcmx.size(); i++) {
			 JxcKcmx jxcKcmxi = jxcKcmx.get(i);
			 String zcKucun = jxcKcmxi.getZcKucun();
			 String zcNew = jxcKcmxi.getZcNew();
			 double total = Double.parseDouble(zcKucun)*Double.parseDouble(zcNew);
			 totalall = totalall+total;
		 }

		 System.out.println("总金额为: " + totalall);
		 return Result.OK(String.valueOf(totalall));
	 }
	/**
	 *   添加
	 *
	 * @param jxcKcmx
	 * @return
	 */
	@AutoLog(value = "库存明细-添加")
	@ApiOperation(value="库存明细-添加", notes="库存明细-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JxcKcmx jxcKcmx) {
		Date date = new Date();
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String id = "ZC"+df2.format(date);
		jxcKcmx.setId(id);
		jxcKcmxService.save(jxcKcmx);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param jxcKcmx
	 * @return
	 */
	@AutoLog(value = "库存明细-编辑")
	@ApiOperation(value="库存明细-编辑", notes="库存明细-编辑")

	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JxcKcmx jxcKcmx) {
		String zcOld = jxcKcmx.getZcOld();
		String zcSize = jxcKcmx.getZcSize();
		String zcNew = jxcKcmx.getZcNew();
		if (zcSize.contains("折")){
		if (zcSize.equals("八折")){
			jxcKcmx.setZcNew(String.valueOf(Double.parseDouble(zcOld)*Double.parseDouble("0.8")));
		}
		if (zcSize.equals("九折")){
			jxcKcmx.setZcNew(String.valueOf(Double.parseDouble(zcOld)*Double.parseDouble("0.9")));
		}
		if (zcSize.equals("无折扣")){
			jxcKcmx.setZcNew(String.valueOf(Double.parseDouble(zcOld)*Double.parseDouble("1")));
		}
		}else {
			jxcKcmx.setZcNew(String.valueOf(Double.parseDouble(zcOld)*Double.parseDouble(zcSize)));
		}

		jxcKcmxService.updateById(jxcKcmx);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存明细-通过id删除")
	@ApiOperation(value="库存明细-通过id删除", notes="库存明细-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jxcKcmxService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库存明细-批量删除")
	@ApiOperation(value="库存明细-批量删除", notes="库存明细-批量删除")

	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcKcmxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "库存明细-通过id查询")
	@ApiOperation(value="库存明细-通过id查询", notes="库存明细-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JxcKcmx> queryById(@RequestParam(name="id",required=true) String id) {
		JxcKcmx jxcKcmx = jxcKcmxService.getById(id);
		if(jxcKcmx==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jxcKcmx);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jxcKcmx
    */

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcKcmx jxcKcmx) {
        return super.exportXls(request, jxcKcmx, JxcKcmx.class, "库存明细");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcKcmx.class);
    }

}
